$( function () {

    // 图片加载效果
    $( "#content img" ).lazyload( {
        effect:"fadeIn",
        failurelimit:30
    });

    $( ".navi li" ).hover( function () {
        $( this ).find( 'ul:first' ).slideDown( "fast" ).css( {visibility: "visible", display: "block"} );
    }, function () {
        $( this ).find( 'ul:first' ).slideUp( "fast" ).css( {visibility: "hidden"} );
    });

    $( ".news div:last" ).css( 'border-right', 'none' );

    // 为所有必填框 添加 红色*
    $( ".required" ).each( function () {
        if ( !$( this ).attr( 'tip' ) ) {
            $( this ).parent().append( '<span style="color: red; vertical-align: middle; padding-left: 5px;">*</span>' );
        }
    });

    // banner
    $("#banner").slidesjs({
        width:980,
        height:280,
        navigation: false,
        play: {
            effect: "slide",
            auto: true,
            interval: 4000
        }
    });

    // 删除最后一条 下边框
    $( "#sidebar .comment:last" ).css( 'border-bottom', 'none' );

    $( ".post-meta-comment a" ).click( function () {
        $( 'html,body' ).animate( {scrollTop:$( '.comment:first' ).offset().top}, 800 );
        return false;
    });

    $( "#AddFavorite" ).click( function () {
        AddFavorite( document.location, document.title );
        return false;
    });
    $( "#sethome" ).click( function () {
        setHomepage( document.location );
        return false;
    });
    $( ".comment-reply" ).click( function () {
        $( 'html,body' ).animate( {scrollTop:$( '#reply' ).offset().top}, 800 );
        var html = $( this ).attr( 'lou' ) + '楼';
        $( "#reply textarea" ).val( "@" + html ).focus();
        return false;
    });

    // 提交评论
    $( ".comment-button" ).click( function () {
        if ( !validate() ) return false;
        var obj = $( this );
        var form = obj.parents( 'form' );
        var url = form.attr( 'action' );
        var data = form.serialize();
        obj.attr( 'disabled', true );

        var loading = '<span id="loading" style="background: url(/content/images/loading.gif) no-repeat left center; padding-left:18px; margin-left: 10px; vertical-align: middle; ">正在提交, 请稍候...</span>';
        var error = '<span id="error" style="background: url(/content/images/error.gif) no-repeat left center; margin-left: 10px; padding-left: 15px;color: red;vertical-align: middle;"></span>';

        obj.parent().find( "span" ).remove();
        obj.after( loading );

        $.post( url, data, function ( result ) {
            if ( result == 'success' ) {
                window.setTimeout( function () {
                    window.location.reload();
                }, 1500 );
                return;
            }
            window.setTimeout(function(){
                obj.parent().find( "span" ).remove();
                obj.after( error );
                $( "#error" ).html( result );
                obj.attr( 'disabled', false );
                obj.parent().find( 'span' ).fadeOut( 5000, function () { obj.parent().find( 'span' ).remove(); });
            }, 1500 );
        });
    });

    $(".case li" ).hover( function () {
        $(this ).find(".desc" ).fadeIn();
    }, function () {
        $(this ).find(".desc" ).fadeOut();
    });
});